package com.zmkm.controller.user;

import com.zmkm.common.Const;
import com.zmkm.common.ServerResponse;
import com.zmkm.entity.Housekeeper;
import com.zmkm.service.IUserService;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/admin/" })
public class AdminController {
	@Autowired
	private IUserService iUserService;

	@RequestMapping(value = { "warehouse" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView warehouse(HttpServletRequest request) {
		return verify(Const.WARE, "admin_warehouse/warehouse", request);
	}

	@RequestMapping(value = { "warehouse_put_in" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView warehouse_put_in(HttpServletRequest request) {
		return verify(Const.WARE, "admin_warehouse/warehouse_put_in", request);
	}

	@RequestMapping(value = { "warehouse_export" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView warehouse_export(HttpServletRequest request) {
		return verify(Const.WARE, "admin_warehouse/warehouse_export", request);
	}

	@RequestMapping(value = { "warehouse_select" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView warehouse_select(HttpServletRequest request) {
		return verify(Const.WARE, "admin_warehouse/warehouse_select", request);
	}

	@RequestMapping(value = { "product_use" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView product_use(HttpServletRequest request) {
		return verify(Const.SERVER, "admin_product/product_use", request);
	}

	@RequestMapping(value = { "product_use_register" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView product_use_register(HttpServletRequest request) {
		return verify(Const.SERVER, "admin_product/product_use_register", request);
	}

	@RequestMapping(value = { "product_use_select" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView product_use_select(HttpServletRequest request) {
		return verify(Const.SERVER, "admin_product/product_use_select", request);
	}

	@RequestMapping(value = { "repeatedly_visit" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView repeatedly_visit(HttpServletRequest request) {
		return verify(Const.SERVER, "admin/repeatedly_visit", request);
	}

	@RequestMapping(value = { "housekeeper" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView housekeeperManage(HttpServletRequest request) {
		return verify(Const.ADMAX, "admin_housekeeper/housekeeper_manage", request);
	}

	@RequestMapping(value = { "housekeeper_insert" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView housekeeperInsert(HttpServletRequest request) {
		Integer gradeId = ((Housekeeper) request.getSession().getAttribute("housekeeper")).getUser_grade();
		if(Const.WARE.contains(gradeId)){
			request.getSession().setAttribute("insertGrade", "11");
			return verify(Const.WARE, "admin_housekeeper/housekeeper_insert", request);
		}else if (Const.SERVER.contains(gradeId)) {
			request.getSession().setAttribute("insertGrade", "21");
			return verify(Const.SERVER, "admin_housekeeper/housekeeper_insert", request);
		}
		return verify(Const.ADMAX, "admin_housekeeper/housekeeper_insert", request);
	}

	@RequestMapping(value = { "housekeeper_select" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView housekeeperSelect(HttpServletRequest request) {
		return verify(Const.ADMAX, "admin_housekeeper/housekeeper_select", request);
	}

	@RequestMapping(value = { "service" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView serviceAd(HttpServletRequest request) {
		return verify(Const.ADMAX, "admin/service", request);
	}

	@RequestMapping(value = { "productManage" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView productManage(HttpServletRequest request) {
		return verify(Const.ADMAX, "admin/productManage", request);
	}

	public ModelAndView verify(ArrayList<Integer> grade, String viewName, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		Housekeeper data = (Housekeeper) request.getSession().getAttribute("housekeeper");
		if (data == null) {
			mv.addObject("serverResponse", ServerResponse.createByErrorMessage("未登陆，请登陆后在进行操作！"));
			mv.setViewName("login");
			return mv;
		}
		if (data != null) {
			ServerResponse verify = this.iUserService.userNameAndpassWordVerify(data.getUserName(), data.getPassWord());
			if (verify.getStatus() == 1) {
				mv.addObject("serverResponse", verify);
				mv.setViewName("login");
				return mv;
			}
			if (!(grade.contains(data.getUser_grade()) || Const.ADMAX.contains(data.getUser_grade()))) {
				mv.addObject("serverResponse", ServerResponse.createByErrorMessage("没有这个操作权限!"));
				request.getSession().setAttribute("housekeeper", data);
				mv.setViewName("index");
				return mv;
			}
		}
		mv.setViewName(viewName);
		return mv;
	}
}

/*
 * Location: C:\Users\Administrator\Desktop\web\zmkm_oa\WEB-INF\classes\
 * Qualified Name: com.zmkm.controller.user.AdminController JD-Core Version:
 * 0.7.0.1
 */